﻿using System;
using System.Collections.Generic;
using JetBrains.Annotations;

namespace com.susi.util.toolkit {
    public class EqualityComparerAdapter<T> : System.Collections.Generic.EqualityComparer<T> {
        private Func<T, T, bool> EqualsComparer;
        private Func<T, int>     HashCodeGetter;

        public EqualityComparerAdapter([NotNull] Func<T, int> hashCodeGetter, [NotNull] Func<T, T, bool> equalsComparer)
        {
            HashCodeGetter = hashCodeGetter ?? throw new ArgumentNullException(nameof(hashCodeGetter));
            EqualsComparer = equalsComparer ?? throw new ArgumentNullException(nameof(equalsComparer));
        }

        public override bool Equals(T x, T y)
        {
            return EqualsComparer(x, y);
        }

        public override int GetHashCode(T obj)
        {
            return HashCodeGetter(obj);
        }
    }
}